/**
 * 通用uni-app网络请求
 * 基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
 */
import tool from '@/tool/tool'
import $config from '@/config/index.config.js';
import api from '@/common/http.api.js'
export default {
	config: {
		// baseUrl: "http://192.168.1.11:1080/",
		baseUrl: $config.baseUrl+'index.php',
		header: {
			// 'Content-Type': 'application/json;charset=UTF-8'
			'Content-Type': 'application/x-www-form-urlencoded',
			// 'Authorization':uni.getStorageSync('loginData').token.access_token
		},
		data: {},
		method: "GET",
		dataType: "json",
		/* 如设为json，会对返回的数据做一次 JSON.parse */
		responseType: "text",
		success() {},
		fail() {},
		complete() {}
	},
	interceptor: {
		request: null,
		response: null
	},
	request(options) {
		if (!options) {
			options = {}
		}
		this.config.header.Authorization = uni.getStorageSync('token') || ''
		this.config.header.token = uni.getStorageSync('token') || ''

		options.baseUrl = options.baseUrl || this.config.baseUrl
		options.dataType = options.dataType || this.config.dataType
		options.url = options.baseUrl + options.url
		options.data = options.data || {}
		options.method = options.method || this.config.method
		return new Promise((resolve, reject) => {
			// uni.showLoading()
			let _config = null
			options.complete = (response) => {
				let statusCode = response.statusCode
				response.config = _config
				if (process.env.NODE_ENV === 'development') {
					if (statusCode === 200) {
					}
				}
				if (this.interceptor.response) {

					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}
				// 统一的响应日志记录
				// _reslog(response)
				let {
					code,
					msg
				} = response.data

				if (code == 401) {
					// tool.msg('登录过期,请重新登录')
					uni.navigateTo({
						url: '/pages/login/login'
					})
					
					// uni.showModal({
					// 	title: '授权过期',
					// 	content: "确认去登录",
					// 	success(res) {
					// 		if (res.confirm) {
								
					// 		}
					// 	}
					// })
					return
					// #ifdef H5
					api.login({
						mytest: 'abc'
					}).then(res => {
						uni.setStorageSync('token', res.data.token.access_token)
						tool.msg('授权成功，请刷新')
					})
					return
					// #endif
					uni.login({
						provider: 'wechat',
						success(res) {
							console.log(res);
							var pid, goods_id;
							if (uni.getStorageSync('pid')) {
								pid = uni.getStorageSync('pid');
								goods_id = uni.getStorageSync('goods_id');
								uni.removeStorageSync('pid')
								uni.removeStorageSync('goods_id')
							}

							api.smallLogin({
								code: res.code,
								pid: pid,
							}).then(res => {
								uni.setStorageSync('token', res.data.token.access_token)
								// if(goods_id){
								// 	uni.navigateTo({
								// 		url: '/subpkg/goods-detail/goods-detail?goods_id=' + goods_id
								// 	})
								// }
								tool.msg('授权成功，请刷新')
							}).catch(err => {
								console.log(err)
							})
						}
					})
					reject(response)
				} else if (code != 1) {
					tool.msg(msg)
					reject(response.data)
				} else {
					resolve(response.data)
				}
			}

			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()

			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}

			uni.request(_config);
		});
	},
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	delete(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}
